<template>
  <div>
    <div style="height:6vh;background-color:#fff;padding: 10px 20px;">
      <span style="line-height: 60px;font-size: 20px;">招聘管理</span>
      <el-button
          type="primary"
          v-if="getUser.level >= 20"
          style="float: right; margin-top: 13px;"
          @click="beforeAddPosition">
        发布职位
      </el-button>
    </div>

    <!-- 职位弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" :before-close="handleClose">
      <el-form label-width="120px" :model="positionForm" :rules="rules" ref="positionForm">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="职位名称" prop="title">
              <el-input v-model="positionForm.title" placeholder="请输入职位名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属部门" prop="departmentId">
              <el-select v-model="positionForm.departmentId" placeholder="请选择部门" style="width:100%">
                <el-option
                    v-for="dept in departmentOptions"
                    :key="dept.id"
                    :label="dept.name"
                    :value="dept.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="职位要求" prop="requirements">
              <el-input
                  type="textarea"
                  :rows="4"
                  v-model="positionForm.requirements"
                  placeholder="请输入职位要求">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="薪资范围" prop="salaryRange">
              <el-input v-model="positionForm.salaryRange" placeholder="请输入薪资范围"></el-input>
            </el-form-item>
          </el-col>

        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="职位状态" prop="status">
              <el-select v-model="positionForm.status" placeholder="请选择状态" style="width:100%">
                <el-option label="招聘中" :value="1"></el-option>
                <el-option label="已结束" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="截止日期" prop="closeDate">
              <el-date-picker
                  v-model="positionForm.closeDate"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  placeholder="选择截止日期"
                  style="width:100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="submitPosition">保存</el-button>
      </template>
    </el-dialog>

    <!-- 职位查询 -->
    <el-card style="margin: 20px;">
      <el-form :inline="true" @submit.native.prevent>
        <el-form-item label="职位名称">
          <el-input v-model="searchForm.title" clearable placeholder="请输入职位名称"></el-input>
        </el-form-item>
        <el-form-item label="部门">
          <el-select v-model="searchForm.departmentId" clearable placeholder="选择部门">
            <el-option
                v-for="dept in departmentOptions"
                :key="dept.id"
                :label="dept.name"
                :value="dept.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" clearable placeholder="选择状态">
            <el-option label="招聘中" :value="1"></el-option>
            <el-option label="已结束" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetSearch">重置</el-button>
          <el-button type="primary" @click="loadPositionList">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 职位列表 -->
    <el-card style="margin: 20px;">
      <el-table :data="positionList">
        <el-table-column label="编号" type="index" align="center"></el-table-column>
        <el-table-column label="职位名称" prop="title" align="center"></el-table-column>
        <el-table-column label="所属部门" align="center">
          <template #default="{row}">
            {{ getDeptName(row.departmentId) }}
          </template>
        </el-table-column>
        <el-table-column label="职位要求" prop="requirements" show-overflow-tooltip align="center"></el-table-column>
        <el-table-column label="薪资范围" prop="salaryRange" align="center"></el-table-column>
        <el-table-column label="状态" align="center">
          <template #default="{row}">
            <el-tag :type="row.status === 1 ? 'success' : 'info'">
              {{ row.status === 1 ? '招聘中' : '已结束' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="发布日期" prop="publishDate" align="center"></el-table-column>
        <el-table-column label="截止日期" prop="closeDate" align="center"></el-table-column>
        <el-table-column label="操作" align="center" fixed="right">
          <template #default="{row}">
            <el-button link type="text" @click="editPosition(row)">编辑</el-button>
            <el-button link type="text" @click="deletePosition(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.current"
          :page-sizes="[3, 4, 5]"
          :page-size="pagination.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          style="margin-top: 20px;">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';
import { Message } from 'element-ui';
import qs from "qs";

export default {
  data() {
    return {
      getUser: this.$getUser(),
      dialogVisible: false,
      dialogTitle: '新增职位',
      positionForm: {
        id: null,
        title: '',
        departmentId: null,
        requirements: '',
        salaryRange: '',
        status: 1,
        closeDate: ''
      },
      rules: {
        title: [{ required: true, message: '请输入职位名称', trigger: 'blur' }],
        departmentId: [{ required: true, message: '请选择部门', trigger: 'change' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }],
        closeDate: [{ required: true, message: '请选择截止日期', trigger: 'change' }],
        requirements: [{ required: true, message: '请输入职位要求', trigger: 'blur' }]
      },
      searchForm: {
        title: '',
        departmentId: null,
        status: null
      },
      positionList: [],
      departmentOptions: [],
      pagination: {
        current: 1,
        size: 3,
        total: 0
      }
    };
  },
  mounted() {
    this.loadPositionList();
    this.loadDepartments();
  },
  methods: {
    // 加载职位列表
    loadPositionList() {
      const data = qs.stringify({
        pageCurrent: this.pagination.current,
        pageSize: this.pagination.size,
        ...this.searchForm
      });

      axios.get(this.$BASE_URL +'/v1/position/list?'+data)
          .then(res => {
            if (res.data.state === 2000) {
              this.positionList = res.data.data.positionList;
              this.pagination.total = res.data.data.total;
            } else {
              Message.error(res.data.message || '获取职位列表失败');
            }
          })
    },

    // 加载部门列表
    loadDepartments() {
      axios.get(this.$BASE_URL +'/v1/department/list')
          .then(res => {
            if (res.data.state === 2000) {
              this.departmentOptions = res.data.data;
            } else {
              Message.error(res.data.message);
            }
          })
    },

    // 获取部门名称
    getDeptName(deptId) {
      const dept = this.departmentOptions.find(d => d.id === deptId);
      return dept ? dept.name : '未知部门';
    },

    // 提交职位数据
    submitPosition() {
      this.$refs.positionForm.validate(valid => {
        if (valid) {
          axios.post(this.$BASE_URL +'/v1/position/save', qs.stringify(this.positionForm))
              .then(res => {
                if (res.data.state === 2000) {
                  Message.success('操作成功');
                  this.dialogVisible = false;
                  this.loadPositionList();
                } else {
                  Message.error(res.data.message || '操作失败');
                }
              })
        }
      });
    },

    // 新增职位
    beforeAddPosition() {
      this.dialogTitle = '新增职位';
      this.dialogVisible = true;
    },

    // 编辑职位
    editPosition(row) {
      this.dialogTitle = '编辑职位';
      this.positionForm = {
        id: row.id,
        title: row.title,
        departmentId: row.departmentId,
        requirements: row.requirements,
        salaryRange: row.salaryRange,
        status: row.status,
        closeDate: row.closeDate
      };
      this.dialogVisible = true;
    },

    // 删除职位
    deletePosition(id) {
      this.$confirm('确定删除该职位吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.post(this.$BASE_URL + `/v1/position/delete/${id}`)
            .then(res => {
              if (res.data.state === 2000) {
                Message.success('删除成功');
                this.loadPositionList();
              } else {
                Message.error(res.data.message || '删除失败');
              }
            })
      })
    },

    // 重置查询
    resetSearch() {
      this.searchForm = {
        title: '',
        departmentId: null,
        status: null
      };
      this.pagination.current = 1;
      this.loadPositionList();
    },

    // 分页大小变化
    handleSizeChange(size) {
      this.pagination.size = size;
      this.loadPositionList();
    },

    // 当前页变化
    handleCurrentChange(current) {
      this.pagination.current = current;
      this.loadPositionList();
    },

    // 关闭对话框
    handleClose() {
      this.dialogVisible = false;
      this.$nextTick(() => {
        if (this.$refs.positionForm) {
          this.$refs.positionForm.resetFields();
        }
      });
    }
  }
};
</script>

<style scoped>

</style>